package org.ki4.kipastelsys

import org.ki4.kipastelsys.auth.*
import grails.plugins.springsecurity.Secured;

class AdminController {
	def springSecurityService
	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def index = {
		request.admin = getCurrentAdmin()
	}

	private getCurrentAdmin() {
		Admin.get(springSecurityService.principal.id)
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[adminInstanceList: Admin.list(params), adminInstanceTotal: Admin.count()]
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def create = {
		def adminInstance = new Admin()
		adminInstance.properties = params
		adminInstance.setEnabled true
		return [adminInstance: adminInstance]
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def save = {
		def adminInstance = new Admin(params)
		adminInstance.setPassword springSecurityService.encodePassword(adminInstance.getPassword())
		if (adminInstance.save(flush: true)) {
			PessoaFuncao.create adminInstance, Funcao.findByAuthority("ROLE_ADMIN"), true
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'admin.label', default: 'Admin'), adminInstance.id])}"
			redirect(action: "show", id: adminInstance.id)
		}
		else {
			render(view: "create", model: [adminInstance: adminInstance])
		}
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def show = {
		def adminInstance = Admin.get(params.id)
		if (!adminInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
			redirect(action: "list")
		}
		else {
			[adminInstance: adminInstance]
		}
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def edit = {
		def adminInstance = Admin.get(params.id)
		if (!adminInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
			redirect(action: "list")
		}
		else {
			return [adminInstance: adminInstance]
		}
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def update = {
		def adminInstance = Admin.get(params.id)
		if (adminInstance) {
			if (params.version) {
				def version = params.version.toLong()
				if (adminInstance.version > version) {

					adminInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [
						message(code: 'admin.label', default: 'Admin')]
					as Object[], "Another user has updated this Admin while you were editing")
					render(view: "edit", model: [adminInstance: adminInstance])
					return
				}
			}
			adminInstance.properties = params
			adminInstance.setPassword springSecurityService.encodePassword(adminInstance.getPassword())
			if (!adminInstance.hasErrors() && adminInstance.save(flush: true)) {
				flash.message = "${message(code: 'default.updated.message', args: [message(code: 'admin.label', default: 'Senha'), ''])}"
				redirect(action: "show", id: adminInstance.id)
			}
			else {
				render(view: "edit", model: [adminInstance: adminInstance])
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
			redirect(action: "list")
		}
	}
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def delete = {
		def adminInstance = Admin.get(params.id)
		if (adminInstance) {
			try {
				adminInstance.delete(flush: true)
				flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
				redirect(action: "list")
			}
			catch (org.springframework.dao.DataIntegrityViolationException e) {
				flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
				redirect(action: "show", id: params.id)
			}
		}
		else {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
			redirect(action: "list")
		}
	}
	
	@Secured(['ROLE_ADMIN', 'IS_AUTHENTICATED_FULLY'])
	def editSenha = {
		def adminInstance = getCurrentAdmin()
		if (!adminInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'admin.label', default: 'Admin'), params.id])}"
			redirect(action: "index")
		}
		else {
			return [adminInstance: adminInstance]
		}
	}
}
